package com.tang.consumer.netty.codec.json;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author Tang
 * @classname JSONDecoder
 * @description [ TODO ]
 * @date 2021/1/19 23:21
 */
@Slf4j
public class JSONDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        log.info("解码器执行...");
        int len = in.readInt();
        byte[] bt = new byte[len];
        in.readBytes(bt);
        ProtocolMessage protocolMessage = new ProtocolMessage();
        protocolMessage.setLen(len);
        protocolMessage.setMessage(bt);
        out.add(protocolMessage);
    }

}